//index.js  
//获取应用实例  
var bsurl = require('../../../utils/baseUrl.js');
var netUtils = require('../../../utils/netUtils.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments: [],
    goods: {},
    cartStr: ["加入购物车", "取消加入"],
    cartIndex: 0,
    offset: 0,
    limit: 20,
    first:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var value = wx.getStorageSync('goods');
    this.setData({
      goods: value
    });
    this.hasAdded();
    this.loadComment();
  },
  loadComment: function () {
    var that = this;
    netUtils.sendRequest("comment/queryComment.do", { id: that.data.goods.goodsid, offset: that.data.offset, limit: that.data.limit }, (data) => {
      console.log(data)
      if (data.body.length == 0 && !that.data.first) {
        wx.showModal({
          title: '提示',
          content: '无更多评论',
        })
      }
      for(var i in data.body){
        var date = new Date();
        date.setTime(data.body[i].time);
        data.body[i].time = date.toLocaleDateString();
      }
      var list = that.data.comments.concat(data.body);
      that.setData({ comments: list, offset:list.length,first:false })
    })
  },
  //加入购物车
  addToCart: function () {
    //已加
    if (this.data.cartIndex) {
      this.setData({ cartIndex: 0 });
      var list = wx.getStorageSync('goodsList');
      list.pop();
      wx.setStorageSync('goodsList', list);
      console.log(list);
    }
    else {
      this.setData({ cartIndex: 1 });
      var list = wx.getStorageSync('goodsList');
      if (!list)
        list = [];
      list = list.concat(this.data.goods);
      wx.setStorageSync('goodsList', list);
      console.log(list);
    }
  },
  hasAdded: function () {
    var list = wx.getStorageSync('goodsList');
    for (var item in list) {
      if (list[item].goodsid == this.data.goods.goodsid) {
        this.setData({ cartIndex: 1 });
      }
    }
  },
  refesh: function () {

  },
  //刷评论
  loadMore: function () {
    this.loadComment();
  }

})